<?php
	require 'config.php';
	require 'init.php';
	require_once ROOT_DIR.'lib/utils/CRunning.php';
	require_once ROOT_DIR.'lib/bet/betResult.php';
	
	$log = new CLogData();
	// begin check running
	echo "start";
	$log->addlog(date('H:i:s Y/m/d').'start','update_bet.log');
	$running = new CRunning();
	if($running->chkRunning("update_bet.php")){
		$log->addlog(date('H:i:s Y/m/d').' exit because running','update_bet.log');
		exit;
	}
	
	// 3Way - Result
	/* echo "<pre>";
	echo "'ft', 2.67, 'bet_val1', 100, 0, 0".'<br>';
	$result = $betResult->result('ft', 2.67, 'bet_val1', 100, 1, 0);
	print_r($result);
	echo "'ft', 2.67, 'bet_val2', 100, 0, 1".'<br>';
	$result = $betResult->result('ft', 2.67, 'bet_val2', 100, 0, 1);
	print_r($result);
	echo "'ft', 2.67, 'bet_val1', 100, 0, 1".'<br>';
	$result = $betResult->result('ft', 2.0, 'bet_val3', 100, 0, 1);
	print_r($result);
	// Asians Handicap
	echo "'ah', 2.0, 'bet_val1', 100, 0, 1,-0.25".'<br>';
	$result = $betResult->result('ah', 2.0, 'bet_val3', 100, 0, 1,-0.25);
	print_r($result);
	echo "'ah', 2.0, 'bet_val1', 100, 0, 0,-0.25".'<br>';
	$result = $betResult->result('ah', 2.0, 'bet_val1', 100, 0, 0,-0.25);
	print_r($result);
	echo "'ah', 2.0, 'bet_val3', 100, 1, 0,-1.25".'<br>';
	$result = $betResult->result('ah', 2.0, 'bet_val3', 100, 2, 0,-1.25);
	print_r($result);
	// Asians Handicap s
	// Double change
	// Over/Under
	echo "'ou', 2.0, 'bet_val1', 100, 1, 0,1.25".'<br>';
	$result = $betResult->result('ou', 2.0, 'bet_val1', 100, 1, 0,1.25);
	print_r($result);
	echo "'ou', 2.2, 'bet_val3', 100, 1, 0,1.25".'<br>';
	$result = $betResult->result('ou', 2.2, 'bet_val3', 100, 1, 0,1.25);
	print_r($result);
	// Total Score
	echo "'ts', 2.2, 'bet_val1', 100, 1, 0,1.25".'<br>';
	$result = $betResult->result('ts', 2.2, 'bet_val1', 100, 1, 0);
	print_r($result);
	echo "'ts', 2.2, 'bet_val3', 100, 1, 0,1.25".'<br>';
	$result = $betResult->result('ts', 2.2, 'bet_val3', 100, 1, 0);
	print_r($result); */
	$betResult = new betResult();
	$lists =  CTable('match')
							->join('match_bet',E('match.id=match_bet.match_id'))
							->select_all(array('match.match_finish'=>1,'match_bet.result'=>0), 
							array(
								'order_by'=>'`match`.id ASC',
								'page_no'=>1,
								'items_per_page'=>1000,
								'fields'=>'match_bet.id as match_bet_id,`match`.*,match_bet.*'
							)
						);
	$log->addlog(date('H:i:s Y/m/d').'tong:'.count($lists),'update_bet.log');
	foreach ($lists as $bet) {
		$result = $betResult->result($bet['bet_type'], $bet['bet_rate'], 'bet_val2', $bet['bet_point'], $bet['team1_goal'], $bet['team2_goal'],$bet['bet_val2']);
		//update match_bet		
		$log->addlog(date('H:i:s Y/m/d').' bet result:'.$result['bet_result'].'|bet_result_point:'.$result['bet_result_point'],'update_bet.log');
		CTable('match_bet')->update(
				array('bet_result'=>$result['bet_result'],'bet_result_point'=>$result['bet_result_point'],'result'=>1),
				array('id'=>$bet['match_bet_id']));
		if($result['bet_result_point']>0){
			CTable('user')->update(
					array('point'=>E('point+'.$result['bet_result_point'])),
					array('id'=>$bet['user_id']));
		}
		//update point
	}
	?>